﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IForm_EditVisualStyle</summary>
    public class IForm_EditVisualStyle : IForm
    {
        public IVisualStyle Value; // Objekt pre editaciu
        public bool CanEditFont; // Priznaky zobrazenie editacnych zon
        public bool CanEditForeColor;
        public bool CanEditBackColor;
        public bool CanEdit3DLook; 

        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_OK;
        protected IButton FBtn_Cancel;

        protected IGroupBox FGrp_Font;
        protected ICheckBox FCheck_UseFont;
        protected ITextBox FText_FontName;
        protected IButton FBtn_ChooseFont;

        protected IGroupBox FGrp_ForeColor;
        protected ICheckBox FCheck_UseForeColor;
        protected IPanel FPanel_ForeColor;

        protected IGroupBox FGrp_BackColor;
        protected ICheckBox FCheck_UseBackColor;
        protected IPanel FPanel_BackColor;

        protected IGroupBox FGrp_Look3D;
        protected ICheckBox FCheck_UseLook3D;
        protected ICheckBox FCheck_Look3D;

        protected IToolTip FToolTip;
        protected System.Drawing.Font FFont;

        /// <summary>Konstruktor</summary>
        public IForm_EditVisualStyle()
        {
            Value = new IVisualStyle(); // Inicializacia
            FFont = null;
        }
        /// <summary>Dispose metoda</summary>
        /// <param name="disposing">parameter</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing); // Volam predchodcu
            Value = (IVisualStyle)ICore.DisposeObject(Value); // Zrusim objekt
            FFont = (System.Drawing.Font)ICore.DisposeObject(FFont);
        }

        /// <summary>Vytvorenie obsahu formulara</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; // Nastavenie vlastnosti
            MinimizeBox = MaximizeBox = false; ShowIcon = true; ShowInTaskbar = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

            Width = 400; Height = 440; System.Drawing.Size CS = ClientSize; // Ziskam vnutorne rozmery

            int GrpCount = 0; // Urcim pocet group boxs
            if (CanEditFont) GrpCount++;
            if (CanEditBackColor) GrpCount++;
            if (CanEditForeColor) GrpCount++;
            if (CanEdit3DLook) GrpCount++;

            System.Windows.Forms.Control[] GrpCtrls = new System.Windows.Forms.Control[GrpCount];
            int GrpIndex = 0; int CurrentY = 10;

            // Vytvorim groupbox
            IGroupBox FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);
            FBtn_Cancel = new IButton(lang.FORM_EDIT_VISUAL_STYLE_TEXT_2); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_EDIT_VISUAL_STYLE_TEXT_1, Event_BtnOKClick); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);

            System.Drawing.Size CS1 = FGrp.ClientSize;

            if (CanEditFont)
            {
                // Oblast pre zmenu fontu
                FGrp_Font = new IGroupBox(); FGrp_Font.SetBounds(10, CurrentY, CS1.Width - 20, 70); // Font
                FCheck_UseFont = new ICheckBox(lang.FORM_EDIT_VISUAL_STYLE_TEXT_3); IControl.PlaceControl_Inside_LeftTop_LeftTop(FCheck_UseFont, FGrp_Font, 10, 16);
                FText_FontName = new ITextBox(); FText_FontName.ReadOnly = true; IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_FontName, FCheck_UseFont, 0, 0, CS1.Width - 90);
                FBtn_ChooseFont = new IButton("...", null, false); FBtn_ChooseFont.Width = 40; FBtn_ChooseFont.Height = FText_FontName.Height; IControl.PlaceControl_Outside_LeftTop_TopRight(FBtn_ChooseFont, FText_FontName, 0, 10, 40, FText_FontName.Height);
                FGrp_Font.Controls.AddRange(new System.Windows.Forms.Control[] { FCheck_UseFont, FText_FontName, FBtn_ChooseFont });
                CurrentY = FGrp_Font.Bottom + 10; GrpCtrls[GrpIndex++] = FGrp_Font;
            }

            if (CanEditForeColor)
            {
                // Oblast pre zmenu farby pisma
                FGrp_ForeColor = new IGroupBox(); FGrp_ForeColor.SetBounds(10, CurrentY, CS1.Width - 20, 76);
                FCheck_UseForeColor = new ICheckBox(lang.FORM_EDIT_VISUAL_STYLE_TEXT_4); IControl.PlaceControl_Inside_LeftTop_LeftTop(FCheck_UseForeColor, FGrp_ForeColor, 10, 16);
                FPanel_ForeColor = new IPanel(); IControl.PlaceControl_Outside_LeftTop_LeftBottom(FPanel_ForeColor, FCheck_UseForeColor, 0, 0, CS1.Width - 50, 24); FPanel_ForeColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                FGrp_ForeColor.Controls.AddRange(new System.Windows.Forms.Control[] { FCheck_UseForeColor, FPanel_ForeColor });
                CurrentY = FGrp_ForeColor.Bottom + 10; GrpCtrls[GrpIndex++] = FGrp_ForeColor;
            }

            if (CanEditBackColor)
            {
                // Oblast pre zmenu farby pozadia
                FGrp_BackColor = new IGroupBox(); FGrp_BackColor.SetBounds(10, CurrentY, CS1.Width - 20, 76);
                FCheck_UseBackColor = new ICheckBox(lang.FORM_EDIT_VISUAL_STYLE_TEXT_5); IControl.PlaceControl_Inside_LeftTop_LeftTop(FCheck_UseBackColor, FGrp_BackColor, 10, 16);
                FPanel_BackColor = new IPanel(); IControl.PlaceControl_Outside_LeftTop_LeftBottom(FPanel_BackColor, FCheck_UseBackColor, 0, 0, CS1.Width - 50, 24); FPanel_BackColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                FGrp_BackColor.Controls.AddRange(new System.Windows.Forms.Control[] { FCheck_UseBackColor, FPanel_BackColor });
                CurrentY = FGrp_BackColor.Bottom + 10; GrpCtrls[GrpIndex++] = FGrp_BackColor;
            }

            if (CanEdit3DLook)
            {
                // Oblast pre zmenu 3D vzhladu
                FGrp_Look3D = new IGroupBox(); FGrp_Look3D.SetBounds(10, CurrentY, CS1.Width - 20, 76);
                FCheck_UseLook3D = new ICheckBox(lang.FORM_EDIT_VISUAL_STYLE_TEXT_6); IControl.PlaceControl_Inside_LeftTop_LeftTop(FCheck_UseLook3D, FGrp_Look3D, 10, 16);
                FCheck_Look3D = new ICheckBox(lang.FORM_EDIT_VISUAL_STYLE_TEXT_7); IControl.PlaceControl_Outside_LeftTop_LeftBottom(FCheck_Look3D, FCheck_UseLook3D, 50, 2);
                FGrp_Look3D.Controls.AddRange(new System.Windows.Forms.Control[] { FCheck_UseLook3D, FCheck_Look3D });
                CurrentY = FGrp_Look3D.Bottom + 10; GrpCtrls[GrpIndex++] = FGrp_Look3D;
            }

            FGrp.Controls.AddRange(GrpCtrls);
            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus();
            FillForm(Value); // Vyplnim formular

            // Nastavim eventy
            if (FCheck_UseFont != null) FCheck_UseFont.CheckedChanged += Event_CheckedChanged;
            if (FCheck_UseForeColor != null) FCheck_UseForeColor.CheckedChanged += Event_CheckedChanged;
            if (FCheck_UseBackColor != null) FCheck_UseBackColor.CheckedChanged += Event_CheckedChanged;
            if (FCheck_UseLook3D != null) FCheck_UseLook3D.CheckedChanged += Event_CheckedChanged;
            if (FBtn_ChooseFont != null) FBtn_ChooseFont.Click += Event_BtnClick_ChooseFont;
            if (FPanel_BackColor != null) FPanel_BackColor.DoubleClick += Event_DblClick_PanelBackColor;
            if (FPanel_ForeColor != null) FPanel_ForeColor.DoubleClick += Event_DblClick_PanelForeColor;

            FToolTip = new IToolTip();  // Vytvorim tooltip
            if (FPanel_ForeColor != null) FToolTip.SetToolTip(FPanel_ForeColor, lang.FORM_EDIT_VISUAL_STYLE_TEXT_8);
            if (FPanel_BackColor != null) FToolTip.SetToolTip(FPanel_BackColor, lang.FORM_EDIT_VISUAL_STYLE_TEXT_8);

            int H = FGrp.Height - CurrentY;
            FGrp.Height -= H; FBtn_OK.Top -= H; FBtn_Cancel.Top -= H; Height -= H; // Upravim formular aby pasoval na vytvoreny pocet controls

            return true; // Vsetko OK
        }
        /// <summary>Aktualizacia stavu</summary>
        protected void UpdateState()
        {
            if (FText_FontName != null) FText_FontName.Enabled = FCheck_UseFont.Enabled & FCheck_UseFont.Checked;
            if (FBtn_ChooseFont != null) FBtn_ChooseFont.Enabled = FCheck_UseFont.Enabled & FCheck_UseFont.Checked;
            if (FPanel_ForeColor != null) FPanel_ForeColor.Enabled = FCheck_UseForeColor.Enabled & FCheck_UseForeColor.Checked;
            if (FPanel_BackColor != null) FPanel_BackColor.Enabled = FCheck_UseBackColor.Enabled & FCheck_UseBackColor.Checked;
            if (FCheck_Look3D != null) FCheck_Look3D.Enabled = FCheck_UseLook3D.Enabled & FCheck_UseLook3D.Checked;
        }
        /// <summary>Vyplnenie okna</summary>
        /// <param name="pValue">objekt</param>
        protected void FillForm(IVisualStyle pValue)
        {
            if (FText_FontName != null) FText_FontName.Text = pValue.FontDisplayName;
            if (FPanel_BackColor != null) FPanel_BackColor.BackColor = pValue.BackColor;
            if (FPanel_ForeColor != null) FPanel_ForeColor.BackColor = pValue.ForeColor;
            if (FCheck_Look3D != null) FCheck_Look3D.Checked = pValue.Look3D;
            if (FCheck_UseFont != null) FCheck_UseFont.Checked = pValue.UseFont;
            if (FCheck_UseForeColor != null) FCheck_UseForeColor.Checked = pValue.UseForeColor;
            if (FCheck_UseBackColor != null) FCheck_UseBackColor.Checked = pValue.UseBackColor;
            if (FCheck_UseLook3D != null) FCheck_UseLook3D.Checked = pValue.UseLook3D;
            FFont = (System.Drawing.Font)ICore.DisposeObject(FFont);
            FFont = (System.Drawing.Font)pValue.Font.Clone();
            UpdateState(); // Aktualizujem stav
        }
        /// <summary>Nacitanie </summary>
        /// <param name="pValue"></param>
        protected void ReadForm(IVisualStyle pValue)
        {
            pValue.Release(); // Uvolnim aktualny obsah
            if ((FCheck_UseFont != null) && (FCheck_UseFont.Checked))// Pouzit font?
            {
                pValue.UseFont = true;
                pValue.Font = FFont;
            }
            if ((FCheck_UseBackColor != null) && (FCheck_UseBackColor.Checked)) // Pouzit farbu pozadia?
            {
                pValue.BackColor = FPanel_BackColor.BackColor;
                pValue.UseBackColor = true;
            }
            if ((FCheck_UseForeColor != null) && (FCheck_UseForeColor.Checked)) // Pouzit farbu pisma?
            {
                pValue.ForeColor = FPanel_ForeColor.BackColor;
                pValue.UseForeColor = true;
            }
            if ((FCheck_UseLook3D != null) && (FCheck_UseLook3D.Checked)) // Pouzit 3D efekty?
            {
                pValue.UseLook3D = true;
                pValue.Look3D = FCheck_Look3D.Checked;
            }
        }

        /// <summary>Event: CheckedChanged</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_CheckedChanged(object pSender, EventArgs e)
        {
            UpdateState();
        }
        /// <summary>Event: OnClick pre tlacidlo FBtn_ChooseFont</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnClick_ChooseFont(object pSender, EventArgs e)
        {
            if (IApp.ShowEditor_ChooseFont(ref FFont)) // Volam editor
            {
                FText_FontName.Text = ICore.GetFontDisplayName(FFont);
            }
        }
        /// <summary>Event: OnDblClick pre panel FPanel_BackColor</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_DblClick_PanelBackColor(object pSender, EventArgs e)
        {
            System.Drawing.Color C = FPanel_BackColor.BackColor;
            if (!IApp.ShowEditor_ChooseColor(ref C)) return; // Volam vyber farby
            FPanel_BackColor.BackColor = C;
        }
        /// <summary>Event: OnDblClick pre panel FPanel_ForeColor</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_DblClick_PanelForeColor(object pSender, EventArgs e)
        {
            System.Drawing.Color C = FPanel_ForeColor.BackColor;
            if (!IApp.ShowEditor_ChooseColor(ref C)) return; // Volam vyber farby
            FPanel_ForeColor.BackColor = C;
        }
        /// <summary>Event: OnClick pre tlacidlo FBtn_OK</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnOKClick(object pSender, EventArgs e)
        {
            ReadForm(Value); // Nacitam hodnoty
            DialogResult = System.Windows.Forms.DialogResult.OK; // Ukoncim dialog
        }

    }
}
